package com.angel.web.health;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.angel.core.util.JsonUtil;
import com.angel.core.util.Pager;
import com.angel.health.BuzCity;
import com.angel.health.BuzInstitutions;
import com.angel.health.BuzMedical;
import com.angel.provider.health.BuzMedicalProvider;


/**
 * @author hanlinxiu
 *
 */

@Controller
@RequestMapping("/medical")
@SessionAttributes("user")
public class BuzMedicalController {

	@Autowired
	private BuzMedicalProvider provider;
	
	/**
	 * 查询机构列表
	 * 
	 * @param currentPage
	 * @param pageSize
	 * @param buzInstitutions
	 * @param modelMap
	 * @param searchParam
	 * @return
	 */
	@RequestMapping("/organization")
	public String findAll(BuzMedical buzMedical, HttpServletRequest request, Pager pager){
		
		pager = provider.findAll(buzMedical, pager);
		
		request.setAttribute("data", pager);
		
		return "institutions/organization";
		
	}
	
	
	/**
	 * 体检库编辑果
	 * 
	 * @param id id不为空则是查看或编辑
	 * @param from edit：编辑  check：查看
	 * @return
	 */
	@RequestMapping("/toAddMedical")
	public String toAddMedical(Integer id, String from, ModelMap modelMap){
		
		BuzMedical buzMedical = null;
		
		if(id != null){
			buzMedical = provider.findById(id);
		}
			
		modelMap.put("from", from);
		modelMap.put("data", buzMedical);
		
		return "institutions/organization_add";
	}
	
	
	/**
	 * 新增，更新机构
	 * 
	 * @param buzInstitutions
	 * @return  
	 */
	@RequestMapping("/addMedical")
	@ResponseBody
	public Map<String, Object> addMedical(@ModelAttribute("user") String user, @ModelAttribute("BuzMedical")BuzMedical buzMedical){
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		
		if(buzMedical.getName()==null || buzMedical.getName().trim().equals("")){
			map.put("status", "error");
			map.put("msg", "体检库名称不能为空!");
			return map;
		}
			
		if(buzMedical.getInstitutionsName()==null || buzMedical.getInstitutionsName().trim().equals("")){
			map.put("status", "error");
			map.put("msg", "体检机构名称不能为空!");
			return map;
		}
		
		if(buzMedical.getId() == null){
			buzMedical.setCreateBy(user);
			provider.insert(buzMedical);
		}else{
			buzMedical.setUpdateBy(user);
			provider.updateById(buzMedical);
		}
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	@RequestMapping("/getAllMedical")
	@ResponseBody
	public Map<String, Object> getAllMedical(){
		
		Map<String, Object> map = new HashMap<>();
		
		BuzMedical buzMedical = new BuzMedical();
		buzMedical.setEnable(1);
		List<Map<String,Object>> list = provider.findAll(buzMedical);
		
		map.put("list", list);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	@RequestMapping("/getAllMedicalAndDepartment")
	@ResponseBody
	public Map<String, Object> getAllMedicalAndDepartment(BuzMedical buzMedical){
		
		Map<String, Object> map = new HashMap<>();
		
		buzMedical.setEnable(1);
		List<BuzMedical> list = provider.selectAllMedicalAndDepartment(buzMedical);
		
		map.put("list", list);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}

}
